package jeecg.business.controller.site;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import jeecg.business.entity.site.SiteEntity;
import jeecg.business.thread.task.ExecTask;

import org.apache.log4j.Logger;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.util.DataUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.clean.CommonHtmlCleanUtil;
import org.jeecgframework.core.util.clean.HtmlCleanBaseUtil;
import org.jeecgframework.core.util.clean.bean.FetchListInfo;
import org.jeecgframework.core.util.clean.bean.HtmlParseResult;
import org.jeecgframework.core.util.http.HttpClientUtil;
import org.jsoup.helper.DataUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import antlr.StringUtils;
@Controller
@RequestMapping("/testSiteController")
public class TestSiteController extends BaseController{
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(TestSiteController.class);
	@RequestMapping(params = "test")
	@ResponseBody
	public AjaxJson test(SiteEntity site, HttpServletRequest request) {
		logger.info("测试站点");
		AjaxJson j = new AjaxJson();
		String result = "";
		boolean success = true;
		try{
			String keyword = request.getParameter("test_keyword");
			if(StringUtil.isNotEmpty(keyword)) keyword = "阅兵";
			result += "1、测试关键词："+keyword+"<br>";
			if("wx".equals(site.getJclx()) || "wb".equals(site.getJclx()) || "dfcfw".equals(site.getId()) || "people".equals(site.getId())){
				result = "此类属于定制化网站,不用进行测试!";
			}else if("ssyq".equals(site.getJclx())){	//搜索引擎
				String basehref = site.getZddz();
				String word;
				word = URLEncoder.encode(keyword,site.getWyzfj());
				basehref = basehref.replace(ExecTask.KEYWORD_FLAG, word);
				result += "2、测试搜索url："+basehref+"<br>";
				String href = basehref;
				if(!href.startsWith("http")) href = site.getGdz() + href;
				FetchListInfo info = new FetchListInfo();
				TagNode root = null;
				TagNode node = null;
				HtmlCleaner cleaner = new HtmlCleaner();
				result += "2.1、获取"+basehref+"源代码<br>";
				String html = HttpClientUtil.getSourceFromUrl(href, site.getWyzfj() , null);
				result += "-----获取"+basehref+"源代码-----成功<br>";
				result += "2.2、转换网页源代码<br>";
				root = cleaner.clean(html);
				result += "------转换网页源代码-----成功<br>";
				result += "2.3、测试内容链接<br>";
				Object[] ns=root.evaluateXPath(site.getLbtq());
				List<String> hreflist = new ArrayList<String>();
				List<Date> timeList = new ArrayList<Date>();
				
				if(ns != null && ns.length >0){
					TagNode childNode = null;
					int position = 0;
					for(int i=0;i<ns.length;i++){
						position = i+1;
						try{
							childNode = (TagNode)ns[i];
							
							/**
							 * 标题
							 */
							result += "2.3.1."+position+"、 获取标题<br>";
							
							String title = null;
							if(StringUtil.isNotEmpty(site.getZwbt())){
								title = HtmlCleanBaseUtil.getTagContent(childNode, site.getZwbt());
							}else{
								title = HtmlCleanBaseUtil.getTitle(node);
							}
							result += "----- 获取标题："+title+" ---成功<br>";
							
							/**
							 * 时间
							 */
							result += "2.3.2."+position+"、 获取列表时间<br>";
							String time = null;
							String timeXpath = null;
							String timeReg = null;
							if(StringUtil.isNotEmpty(site.getZwsj())){
								if(site.getZwsj().indexOf("@|@")>0){
									String [] array = site.getZwsj().split("@\\|@");
									timeXpath = array[0];
									timeReg = array[1];
									time = HtmlCleanBaseUtil.getTagContent(childNode, timeXpath);
		//							System.out.println(time);
									Pattern p = Pattern.compile(timeReg);
									Matcher m = p.matcher(time);
									if(m.find()) 
										time = m.group();
								}else{
									time = HtmlCleanBaseUtil.getTagContent(childNode, site.getZwsj());
								}
								if(StringUtil.isNotEmpty(time)){
									Date eventdate = DataUtils.parseDate(time);
									result += "----- 获取时间："+DataUtils.formatDate(eventdate, "yyyy年MM月dd日")+" ---成功<br>";
								}
							}
							
							/**
							 * 正文
							 */
//							result += "2.3.3."+position+"、 获取正文内容<br>";
							String zwnrXpath = "//body";
							if(StringUtil.isNotEmpty(site.getZwnr())) zwnrXpath = site.getZwnr();
							
							result += "2.3.3."+position+"、 获取正文链接<br>";
							/**
							 * 正文连接
							 */
							if(StringUtil.isNotEmpty(site.getZwlj())){
								String lyurl = HtmlCleanBaseUtil.getAttributeValue(childNode, site.getZwlj() , "href");
								result += "----- 获取链接："+lyurl+" ---成功<br>";
							}
							
							
						}catch(Exception e){
							e.printStackTrace();
							System.out.println("抓取失败："+href);
						}
					}
					result += "------根据'获取列表xpath'来锁定链接-----成功<br>";
				}
				
				/**
				 * 下一页
				 */
				result += "2.4、获取下一页地址<br>";
				String nextPage = HtmlCleanBaseUtil.getAttributeValue(root, site.getLbxyy() , "href");
				result += "获取下一页地址："+nextPage+"结束<br>";
				
				
				
			}else{		//其他类型
				String basehref = site.getZddz();
				String word;
				word = URLEncoder.encode(keyword,site.getWyzfj());
				basehref = basehref.replace(ExecTask.KEYWORD_FLAG, word);
				result += "2、测试搜索url："+basehref+"<br>";
				String href = basehref;
//				if(!href.startsWith("http")) href = site.getGdz() + href;
				if(!href.startsWith("http")) href = StringUtil.getAbsoluteURL(site.getGdz() , href);
				FetchListInfo info = new FetchListInfo();
				TagNode node = null;
				HtmlCleaner cleaner = new HtmlCleaner();
				result += "2.1、获取"+basehref+"源代码<br>";
				logger.info("get basehref,"+href);
				String html = HttpClientUtil.getSourceFromUrl(href, site.getWyzfj() , null);
				result += "-----获取"+basehref+"源代码-----成功<br>";
				result += "2.2、转换网页源代码<br>";
				node = cleaner.clean(html);
				result += "------转换网页源代码-----成功<br>";
				result += "2.3、根据'获取列表xpath'来锁定链接<br>";
				Object[] ns=node.evaluateXPath(site.getLbtq());
				List<String> hreflist = new ArrayList<String>();
				List<Date> timeList = new ArrayList<Date>();
				if(ns != null && ns.length >0){
					TagNode cnode = null;
					for(Object cobj : ns){
						cnode = (TagNode)cobj;
						hreflist.add(cnode.getAttributeByName("href"));
					}
					info.setHrefList(hreflist);
				}
				result += "------根据'获取列表xpath'来锁定链接-----成功<br>";
				if("2".equals(site.getSjhqfs())){
					result += "2.4、在列表上获取时间";
					String time = null;
					String timeXpath = null;
					String timeReg = null;
					TagNode timenode = null;
					if(StringUtil.isNotEmpty(site.getZwsj())){
						if(site.getZwsj().indexOf("@|@")>0){
							String [] array = site.getZwsj().split("@\\|@");
							timeXpath = array[0];
							timeReg = array[1];
							Object[] times=node.evaluateXPath(timeXpath);
							if(times != null && times.length>0){
								for(Object cobj : times){
									timenode = (TagNode)cobj;
									time = timenode.getText().toString();
									Pattern p = Pattern.compile(timeReg);
									Matcher m = p.matcher(time);
									if(m.find()) 
										time = m.group();
									Date eventdate = DataUtils.parseDate(time);
									timeList.add(eventdate);
								}
								info.setTimeList(timeList);
							}
							
						}
					}
					result += "------在列表上获取时间-----成功<br>";
				}
				
				result += "2.5、列表结果如下：<br>";
				for(int i=0;i<hreflist.size();i++){
					result += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"2.5."+(i+1)+"、链接："+hreflist.get(i);
					if(timeList != null && timeList.size()>i)
						result += "------时间："+DataUtils.formatDate(timeList.get(i), "yyyy年MM月dd日");
					result +="<br>";
				}
				
				/**
				 * 获取下一页
				 */
				result += "2.6、获取下一页地址<br>";
				if(StringUtil.isNotEmpty(site.getLbxyy())){
					String nextPage = null;
					/**
					 * param
					 */
					if("param".equals(site.getXyyhqfs())){
						String paramBaseHref = basehref;
						if(!basehref.contains("{page}")) basehref = paramBaseHref + "&"+site.getLbxyy()+"={page}";
						basehref = basehref.replace("{page}", (2)+"");
					}else{
						/**
						 * 如果是xpath
						 */
						ns=node.evaluateXPath(site.getLbxyy());
						if(ns != null && ns.length>0){
							nextPage = ((TagNode)ns[0]).getAttributeByName("href");
						}
					}
					
					if(StringUtil.isNotEmpty(nextPage))
//						if(!nextPage.startsWith("http")) nextPage = site.getGdz() + nextPage;
						if(!nextPage.startsWith("http")) nextPage = StringUtil.getAbsoluteURL(site.getGdz() , nextPage);
						result += "下一页地址是："+nextPage+"<br>";
				}
				result += "获取下一页地址结束<br>";
				
				/**
				 * 获取内容
				 */
				result += "3、测试内容链接<br>";
					for(String url : hreflist){
						HtmlCleaner childcleaner = new HtmlCleaner();
						int position = hreflist.indexOf(url)+1;
//						if(!url.toLowerCase().startsWith("http")) url = site.getGdz() + url;
						if(!url.startsWith("http")) url = StringUtil.getAbsoluteURL(site.getGdz() , url);
						result += "3."+position+"、测试第"+(position)+"内容链接："+url+"<br>";
						result += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"3."+position+".1、获取内容页面源代码<br>";
						String childhtml = null;
						childhtml = HttpClientUtil.getSourceFromUrl(url, site.getNyzfj(), null);
						result += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"3."+position+".1、获取内容页面源代码------成功<br>";
						if("1".equals(site.getRctz()) && StringUtil.isNotEmpty(site.getRcljqb())){
							result += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"------需要进行二次跳转------<br>";
							int p = childhtml.indexOf(site.getRcljqb());
							if(p>=0){
								String newhref = childhtml.substring(p+site.getRcljqb().length(),html.indexOf(site.getRcljhb(), p+site.getRcljqb().length()));
								childhtml = HttpClientUtil.getSourceFromUrl(newhref, site.getNyzfj(), null);
							}
							result += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"------需要进行二次跳转------成功<br>";
						}
						
						if("1".equals(site.getZnbm())){
							result += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"------需要进行智能转码------<br>";
							node = childcleaner.clean(childhtml);
							TagNode headNode = (TagNode)node.evaluateXPath("//head")[0];
							String encode = HttpClientUtil.getEncode(childcleaner , headNode);
							if(!site.getNyzfj().equalsIgnoreCase(encode) && encode != null){
								childhtml = HttpClientUtil.getSourceFromUrl(url, encode, null);
							}
							result += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"------需要进行智能转码，页面编码："+encode+"------成功<br>";
						}
						node = childcleaner.clean(childhtml);
						result += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"3."+position+".2、获取内容页标题------<br>";
						String title = null;
						if(StringUtil.isNotEmpty(site.getZwbt())){
							title = HtmlCleanBaseUtil.getTagContent(node, site.getZwbt());
							if(StringUtil.isEmpty(title))
								title = HtmlCleanBaseUtil.getTitle(node);
						}else{
							title = HtmlCleanBaseUtil.getTitle(node);
						}
						title = StringUtil.removeHTMLLable(title);
						result +="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+ "3."+position+".2、获取内容页标题："+title+"------<br>";
						
						result += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"3."+position+".3、获取内容页内容------<br>";
						String zwnrXpath = "//body";
						if(StringUtil.isNotEmpty(site.getZwnr())) zwnrXpath = site.getZwnr();
						String content = HtmlCleanBaseUtil.getTagContent(node, zwnrXpath);
						
						content = StringUtil.removeHTMLLableExe(content);
						result += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"3."+position+".3、获取内容页内容："+content+"------成功<br>";
						
						if(!"2".equals(site.getSjhqfs())){	//获取时间
							result += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"3."+position+".4、获取内容页时间------<br>";
							String time = null;
							String timeXpath = null;
							String timeReg = null;
							if(StringUtil.isNotEmpty(site.getZwsj())){
								if(site.getZwsj().indexOf("@|@")>0){
									String [] array = site.getZwsj().split("@\\|@");
									timeXpath = array[0];
									timeReg = array[1];
									time = HtmlCleanBaseUtil.getTagContent(node, timeXpath);
									Pattern p = Pattern.compile(timeReg);
									Matcher m = p.matcher(time);
									if(m.find()) 
										time = m.group();
								}else{
									time = HtmlCleanBaseUtil.getTagContent(node, site.getZwsj());
								}
								if(StringUtil.isNotEmpty(time)) {
									Date eventdate = DataUtils.parseDate(time);
									result += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"3."+position+".4、获取内容页时间："+DataUtils.formatDate(eventdate, "yyyy年MM月dd日")+"------成功<br>";
								}
							}
						}
					}
				result += "------测试内容链接"+"------成功<br>";
			}
		}catch(Exception e){
			logger.error(e);
			e.printStackTrace();
			success = false;
			result += "测试异常！<br>";
		}
		j.setSuccess(success);
		j.setMsg(result);
		return j;
	}
}
